using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MapFileParser
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ParseMap(textBox1.Text);             
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = "K:\\Dune1999\\Dune 2000\\Dune2000\\Data\\maps";
            dlg.Filter = "Dune 2000 map files (*.map)|*.map";
            
            if (dlg.ShowDialog() == DialogResult.OK)
            {

                textBox1.Text = dlg.FileName;
                
                button1_Click(null, null);                
            }
        }

        private void ParseMap(string fileName)
        {
            int specialTileCount = 0;
            int thinSpiceCount = 0;
            int thickSpiceCount = 0;
            int sandWormCount = 0;
            int spawnPointCount = 0;
            int spiceBloomCount = 0;
            int otherTileCount = 0;

            Map map = new Map(fileName);

            listBox1.Items.Clear();
            List<Tile> tileArray = map.TileArray;
            foreach (Tile tile in tileArray)
            {                
                listBox1.Items.Add(tile.tileIndex.ToString() + " - " + tile.specialIndex.ToString());
                if (tile.specialIndex > 0)
                {
                    specialTileCount++;
                    switch (tile.specialIndex)
                    {
                        case 1:
                            thinSpiceCount++;
                            break;
                        case 2:
                            thickSpiceCount++;
                            break;
                        case 20:
                            sandWormCount++;
                            break;
                        case 23:
                            spawnPointCount++;
                            break;
                        case 45:
                            spiceBloomCount++;
                            break;
                        default:
                            otherTileCount++;
                            break;
                    }
                }
            }

            label1.Text = "Map loaded."
            + "\nMap size is: " + map.Width.ToString() + "x" + map.Height.ToString()
            + "\nTiles loaded: " + map.TileArray.Count.ToString()
            + "\nSpecial Tiles: " + specialTileCount
            + "\nThin Spice Count: " + thinSpiceCount
            + "\nThick Spice Count: " + thickSpiceCount
            + "\nSand Worm Count: " + sandWormCount
            + "\nSpawn Point Count: " + spawnPointCount
            + "\nSpice Bloom Count: " + spiceBloomCount
            + "\nOther Tile Count: " + otherTileCount;                                   

        }
    }
}